package com.yuqing.sentimentapp.workspace.search;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.linkface.common.adaper.CommonViewHolder;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseActivity;
import com.yuqing.sentimentapp.base.BaseObserver;
import com.yuqing.sentimentapp.module.ModuleManager;
import com.yuqing.sentimentapp.widget.xrecyclerview.XRecyclerView;
import com.yuqing.sentimentapp.workspace.search.adapter.YqSearchAdapter;
import com.yuqing.sentimentapp.workspace.search.entity.YqSearchResult;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by shenhe on 2020/4/19.
 */
public class YqSearchResultActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.recyclerView)
    XRecyclerView mRecyclerView;
    @BindView(R.id.empty_view)
    TextView emptyView;
    private YqSearchAdapter mAdapter;
    private List<YqSearchResult.DataBean.ListBean> dataList = new ArrayList<>();
    private int page = 1;
    private String keyword;
    private String from;
    private String to;
    private String source;
    private String region;
    private String city;

    public static void goActivity(Activity activity, String keyword, String from, String to, String source, String region, String city) {
        Intent intent = new Intent(activity, YqSearchResultActivity.class);
        intent.putExtra("keyword", keyword);
        intent.putExtra("from", from);
        intent.putExtra("to", to);
        intent.putExtra("source", source);
        intent.putExtra("region", region);
        intent.putExtra("city", city);
        activity.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_yq_search_result;
    }

    @Override
    public void initPresenter() {
        Intent intent = getIntent();
        if (intent != null) {
            keyword = intent.getStringExtra("keyword");
            from = intent.getStringExtra("from");
            to = intent.getStringExtra("to");
            source = intent.getStringExtra("source");
            region = intent.getStringExtra("region");
            city = intent.getStringExtra("city");
        }
    }

    @Override
    public void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setLoadingMoreEnabled(true);
        mRecyclerView.setPullRefreshEnabled(true);

        mAdapter = new YqSearchAdapter(this, dataList, new CommonViewHolder.onItemCommonClickListener() {
            @Override
            public void onItemClickListener(int position) {

            }

            @Override
            public void onItemLongClickListener(int position) {

            }
        });
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.refresh();
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadYqData(keyword, from, to, source, region, city, page, true);
            }

            @Override
            public void onLoadMore() {
                page++;
                loadYqData(keyword, from, to, source, region, city, page, false);
            }
        });

        loadYqData(keyword, from, to, source, region, city, 1, true);
    }

    private void loadYqData(String keyword, String from, String to, String source, String region, String city, int page, boolean isRefresh) {
        loadYqListData(keyword, from, to, source, region, city, page, 10, isRefresh);
    }

    private void loadYqListData(String keyword, String from, String to, String source, String region, String city, int page, int pageSize, boolean isRefresh) {
        ModuleManager.getInstance().loadYqListResult(keyword, from, to, source, region, city, page, pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<YqSearchResult>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(YqSearchResult yqSearchResult) {
                        stopLoad();
                        if (null == yqSearchResult || null == yqSearchResult.getData()) {
                            if (!isRefresh) {
                                mRecyclerView.setNoMore(true);
                            }
                            return;
                        }
                        if (isRefresh && yqSearchResult.getData().getList().size() != 0) {
                            dataList.clear();
                        }
                        dataList.addAll(yqSearchResult.getData().getList());
                        mAdapter.notifyDataSetChanged();
                        if (dataList.size() == 0) {
                            emptyView.setVisibility(View.VISIBLE);
                        } else {
                            emptyView.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    protected void onFailure(String message) {
                        showShortToast(message);
                        stopLoad();
                    }
                });
    }

    private void stopLoad() {
        mRecyclerView.refreshComplete();
        mRecyclerView.loadMoreComplete();
    }

    @OnClick(R.id.iv_back)
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
